package com.jweb.framework.http.submit;

import java.util.UUID;

import javax.servlet.jsp.JspException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.tags.RequestContextAwareTag;


public class TokenTag extends RequestContextAwareTag {

	
	private static final long serialVersionUID = 6126372078079107077L;
	
	protected static Logger logger = LoggerFactory.getLogger(TokenTag.class);
	
	private String token = null ;

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	@Override
	protected int doStartTagInternal() throws Exception {
		String token = UUID.randomUUID().toString().replace("-","");
		pageContext.getSession().setAttribute("token", token);
		setToken(token);
		StringBuffer sb = new StringBuffer();
		sb.append("<input");
        sb.append(" type=\"" + "hidden" + "\"");
        sb.append(" name=\"" + "token" + "\"");
        sb.append(" value=\"" + token + "\"");
        sb.append(" />");
        pageContext.getOut().println(sb.toString());
		
		return SKIP_BODY;
	}
	
	@Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }
	

}
